package com.cqut.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.dao.JudgesAppointmentMapper;
import com.cqut.dao.JudgesAppointmentMapperExtra;
import com.cqut.dto.LimitShowDTO;
import com.cqut.model.JudgesAppointment;
import com.cqut.model.Role;
import com.cqut.service.IJudgesAppointmentService;

@Service
public class JudgesAppointmentService implements IJudgesAppointmentService {
	
	@Resource(name="judgesAppointmentMapper")
    private JudgesAppointmentMapper judgesAppointmentMapper;

	@Resource(name="judgesAppointmentMapperExtra")
    private JudgesAppointmentMapperExtra JudgesAppointmentMapperExtra;
	
	/**
	 * 初始化大赛列表
	 * @param limitShowDTO
	 * @param errors
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getList(LimitShowDTO limitShowDTO, String competeName) {
		return JudgesAppointmentMapperExtra.getList(limitShowDTO.calculateStart(), limitShowDTO.calculateEnd(), competeName);
	}
	
	/**
	 * 得到赛制流程列表
	 */
	@Override
	public List<Map<String, Object>> getFlowStates() {		
		return JudgesAppointmentMapperExtra.getFlowStates();
	}

	/**
	 * 得到大赛消息条数	
	 * */
	@Override
	public long findCountOfList(String competeName) {
		return JudgesAppointmentMapperExtra.getCountOfList(competeName);
	}

	/**
	 * 得到评委条数
	 * */
	@Override
	public long findCountOfJudgesist(String judgeName) {		
		return JudgesAppointmentMapperExtra.findCountOfJudgesist(judgeName);
	}
	
	/**
	 * 得到评委列表
	 * @param LimitShowDTO
	 * */
	@Override
	public List<Map<String, Object>> getJudgesList(LimitShowDTO limitShowDTO, String judgeName) {
		return JudgesAppointmentMapperExtra.getJudgesList(limitShowDTO.calculateStart(), limitShowDTO.calculateEnd(), judgeName);
	}

	/**
	 * 得到比赛阶段列表
	 * */
	@Override
	public List<Map<String, Object>> getStates() {
		return JudgesAppointmentMapperExtra.getStates();
	}
	
	/**
	 * 新增一条数据
	 * */
	@Override
    public boolean save(List<JudgesAppointment> judges) {
		for(int i = 0; i < judges.size(); i++) {
            if(judgesAppointmentMapper.insert(judges.get(i)) < 0) {
            	return false;
            }
		}
		
		return true;
    }

	/**
	 * 指定评委新增
	 * */
	@Override
	public boolean setJudges(List<Map<String, String>> states, List<Map<String, String>> judges) {
		if(judges == null) return true;
		if(judges.size() == 0) return true;
		
		List<JudgesAppointment> setJudges = new ArrayList<JudgesAppointment>();
		int maxRaterAppointId = getMaxRaterAppointId();
		for(int i = 0; i < states.size(); i++) {
			for(int j = 0; j < judges.size(); j++) {
				JudgesAppointment rater = new JudgesAppointment();
				rater.setProcessResultDetailId(states.get(i).get("process_result_detail_id"));
				rater.setJudgesIds(judges.get(j).get("judges_id"));
				rater.setJudgesNames(judges.get(j).get("judges_name"));
				rater.setRaterAppointId(getNewRaterAppointId(maxRaterAppointId));
				
				setJudges.add(rater);
				maxRaterAppointId++;
			}
		}
		
		// 新增角色
		if(!save(setJudges))
			return false; 
		
		return true;
		
	}

	/**
	 * 生成新的raterAppointId
	 * */
	private String getNewRaterAppointId(int max) {
		String maxNum = Integer.toString(max);
		
		switch(maxNum.length()){
		case 1: return "0000000" + max;
		case 2: return "000000" + max;	
		case 3: return "00000" + max;
		case 4: return "0000" + max;
		case 5: return "000" + max;
		case 6: return "00" + max;
		case 7: return "0" + max;
		}
		
		return "";
	}

	/**
	 * 获得最大的raterAppointId
	 * */
	private int getMaxRaterAppointId() {
		Map<String, Object> maxRaterAppointId = JudgesAppointmentMapperExtra.findMaxRaterAppointId();
		
		int max;
		String temp = "";
		if(maxRaterAppointId != null) {
			temp = maxRaterAppointId.values().toString();
			max = Integer.parseInt(temp.substring(1,temp.length()-1));
		} else {
			max = 0;
		}
		
		max++;
		return max;
	}

	/**
	 * 挑选比赛流程明细id
	 * */
	@Override
	public List<Map<String, String>> selectState(String[] stateList) {
		return JudgesAppointmentMapperExtra.selectState(stateList);
	}

	/**
	 * 挑选评委名称
	 * */
	@Override
	public List<Map<String, String>> selectJudges(String[] judgesList) {
		if(judgesList.length == 0) {
			return null;
		}
		return JudgesAppointmentMapperExtra.selectJudges(judgesList);
	}

	/**
	 * 初始化比赛阶段列表
	 * @param LimitShowDTO
	 * */
	@Override
	public List<Map<String, Object>> getStateList(LimitShowDTO limitShowDTO, String stateName, String competitionId) {
		return JudgesAppointmentMapperExtra.getStateList(limitShowDTO.calculateStart(), limitShowDTO.calculateEnd(), stateName, competitionId);
	}

	/**
	 * 得到比赛阶段消息条数
	 * */
	@Override
	public long findCountOfStateList(String stateName, String competitionId) {
		return JudgesAppointmentMapperExtra.getCountOfStateList(stateName, competitionId);
	}

	/**
	 * 初始化流程列表
	 * @param LimitShowDTO
	 * */
	@Override
	public List<Map<String, Object>> getFlowList(LimitShowDTO limitShowDTO, String comProcessId, String flowName) {
		return JudgesAppointmentMapperExtra.getFlowList(limitShowDTO.calculateStart(), limitShowDTO.calculateEnd(), comProcessId, flowName);
	}

	/**
	 * 得到流程消息条数
	 * */
	@Override
	public long findCountOfFlowList(String comProcessId, String flowName) {
		return JudgesAppointmentMapperExtra.getCountOfFlowList(comProcessId, flowName);
	}

	/**
	 * 根据大赛得到流程
	 * */
	@Override
	public List<Map<String, Object>> getListByCom(String[] competitionId) {
		return JudgesAppointmentMapperExtra.getListByCom(competitionId);
	}

	/**
	 *  根据阶段得到流程
	 * */
	@Override
	public List<Map<String, Object>> getListByPro(String[] comProcessIds) {
		return JudgesAppointmentMapperExtra.getListByPro(comProcessIds);
	}

	/**
	 * 得到已指定评委
	 * */
	@Override
	public List<Map<String, Object>> getAppointJudges(String[] processDetailIds) {
		return JudgesAppointmentMapperExtra.getAppointJudges(processDetailIds);
	}

	/**
	 * 评委指定删除
	 * */
	@Override
	public boolean deleteJudges(String[] deleteJudgesIds ,String[] processDetailIds) {
		if(deleteJudgesIds.length == 0) {
			return true;
		}
		
		return delete(deleteJudgesIds, processDetailIds);
	}
	
	/**
	 * 删除数据
	 * */
	@Override
	public boolean delete(String[] deleteJudgesIds, String[] processDetailIds) {
		for(int i = 0; i < deleteJudgesIds.length; i++) {
			if(JudgesAppointmentMapperExtra.deleteJudges(deleteJudgesIds[i], processDetailIds) < 0) {
				return false;
			}
		}
		
		return true;
	}

	/**
	 * 得到大赛流程id
	 * */
	@Override
	public List<Map<String, Object>> getDetailIds(String[] processIds) {
		return JudgesAppointmentMapperExtra.getDetailIds(processIds);
	}
	
}
